const mix = require('laravel-mix');
const path = require('path')
const {CleanWebpackPlugin} = require("clean-webpack-plugin");
const { hot } = require('minimist')(process.argv.slice(2));

let config = {
    output: {
        publicPath: '/asset-anchor/',
        chunkFilename: `js/[name].${mix.inProduction() ? '[chunkhash].' : ''}js`,
    },
};
if (hot) delete config.output.publicPath;

if (mix.inProduction()) {
    mix.version();
    mix.sourceMaps(false);
    config.plugins = [
        new CleanWebpackPlugin()
    ];
}
/*
* 生产模式增加版本号并关闭sourceMaps
* setPublicPath 决定mainfest.json位置
* */
mix.setResourceRoot('/asset-anchor/')
    .setPublicPath('public/asset-anchor/')
    .alias({
        '~': path.join(__dirname, './resources/anchor'),
    })
    .webpackConfig(config)
    .js('resources/anchor/app.js', 'js')
    .extract([
        'vue',
        'vuex',
        'vue-router',
        'vue-chartjs',
        'echarts',
        'axios',
        'vue-axios',
        'element-ui',
        'moment',
        'lodash',
        'jquery',
        'xlsx',
        'vue-router-tab',
        'vxe-table',
        'xe-utils',
    ], 'js/vendor')
    .sass('resources/sass/backend.scss', 'css/app.css')
    .vue();

/**
 * hot 热加载模式需要拷贝字体文件
 * watch 模式需要使用browserSync
 * */
if (hot) {
    mix.copyDirectory('node_modules/element-ui/lib/theme-chalk/fonts/*', 'public/anchor/fonts/vendor/element-ui/lib/theme-chalk/')
} else if (!mix.inProduction()) {
    mix.browserSync({
        proxy: process.env.APP_URL,
        startPath: '/anchor',
        open: true,
        ghostMode: {
            clicks: false,
            scroll: false,
            forms: false,
        },
        reloadOnRestart: true,
        watchOptions: {
            usePolling: true,
        },
        files: [
            "public/asset-anchor/js/*.js",
            "public/asset-anchor/css/*.css",
        ],
    });
}
